<?php


namespace App\Service;


use App\Constants\SettleAccessType;
use App\Model\Dao\EventOrderDao;
use App\Model\Enum\OrderHandleStatusEnum;
use App\Model\Enum\OrderReviewStatusEnum;
use App\Model\Enum\OrderSettleStatusEnum;
use App\Utils\CustomUtils;
use Hyperf\Database\Query\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Arr;

/**
 * Class EventOrderReviewService
 * 事件订单审核
 *
 * @package App\Service
 */
class EventOrderReviewService {

    /**
     * @var EventOrderDao
     * @Inject()
     */
    private $EventOrderDao;

    /**
     * @var SettleService
     * @Inject()
     */
    private $SettleService;

    protected function _getEventOrderOrFail ($id, $mchIdList) {

        $eventOrder = $this->EventOrderDao->getWhere(function (Builder $query) use ($id, $mchIdList) {
            $query->where('id', $id);
            !is_null($mchIdList) && $query->whereIn('mchId', $mchIdList);
        });

        if (!$eventOrder) {
            throw new \Exception('事件订单不存在');
        }

        return $eventOrder;
    }

    protected function _verifySettleStatus ($item) {
        if ($item['settleStatus'] != 1) {
            throw new \Exception('出入账处理未完成');
        }
    }

    protected function _verifyReviewable ($item, $reviewLevel) {
        switch ($reviewLevel) {
            case 1:
                if ($item['reviewStatus'] != OrderReviewStatusEnum::INIT) {
                    throw new \Exception('不处于待审核状态');
                }
                break;
            case 2:
                if ($item['reReviewStatus'] != OrderReviewStatusEnum::INIT) {
                    throw new \Exception('不处于待审核状态');
                }
                break;
        }
    }

    protected function _parseStatusParams ($reviewLevel, $isApprove, $reviewBy, $remark = '') {

        $update = [];

        switch ($reviewLevel) {
            case 1:
                // 初审
                $update['handleStatus'] = $isApprove ? OrderHandleStatusEnum::FIRST_APPROVE : OrderHandleStatusEnum::FIRST_REJECT;
                $update['reviewStatus'] = $isApprove ? OrderReviewStatusEnum::APPROVE : OrderReviewStatusEnum::REJECT;
                $update['reviewRemark'] = $remark;
                $update['reviewBy']     = $reviewBy;
                $update['reviewTime']   = time();
                break;

            case 2:
                // 复审
                $update['handleStatus']   = $isApprove ? OrderHandleStatusEnum::SECOND_APPROVE : OrderHandleStatusEnum::SECOND_REJECT;
                $update['reReviewStatus'] = $isApprove ? OrderReviewStatusEnum::APPROVE : OrderReviewStatusEnum::REJECT;
                $update['reReviewRemark'] = $remark;
                $update['reReviewBy']     = $reviewBy;
                $update['reReviewTime']   = time();
                break;
        }

        $update['settleStatus'] = OrderSettleStatusEnum::INIT;

        return $update;
    }

    public function handleReview (array $param) {

        $id          = Arr::get($param, 'id');
        $isConfirm   = Arr::get($param, 'isConfirm');
        $remark      = Arr::get($param, 'remark');
        $mchIdList   = Arr::get($param, 'mchIdList');
        $reviewLevel = Arr::get($param, 'reviewLevel');
        $createBy    = Arr::get($param, 'createBy');

        $eventOrder = $this->_getEventOrderOrFail($id, $mchIdList);

        $this->_verifySettleStatus($eventOrder);

        $this->_verifyReviewable($eventOrder, $reviewLevel);

        $update = $this->_parseStatusParams($reviewLevel, $isConfirm, $createBy, $remark);

        // 更新
        $this->EventOrderDao->update($id, $update);

        // 出入账处理
        $this->SettleService->addMq($eventOrder['mchId'], [
            'accessType'   => SettleAccessType::ACCESS_TYPE_EVENT_ORDER_FROZEN, // 统一给8001
            'eventOrderId' => $id,
            'handleStatus' => $update['handleStatus'],
        ]);

        $data = [];
        return CustomUtils::buildSuccess($data);
    }


}
